//
//  BaseTextField.m
//  PostDelivery
//
//  Created by wmz on 2020/6/24.
//  Copyright © 2020 wmz. All rights reserved.
//

#import "BaseTextField.h"

@interface BaseTextField()<UITextFieldDelegate>{
    BOOL had;
}
@end

@implementation BaseTextField

- (void)setShowLine:(BOOL)showLine{
    _showLine = showLine;
    self.line.hidden = !showLine;
}

- (void)awakeFromNib{
    [super awakeFromNib];

    if (!self.fontS) {
        self.fontS = 16;
    }

    if (self.placeholder) {
      NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
         if (self.must) {
             [text addAttributes:@{NSForegroundColorAttributeName:PostColor(0xADADAD)} range:[text.string rangeOfString:text.string]];
             [text addAttributes:@{NSForegroundColorAttributeName:UIColor.redColor} range:[text.string rangeOfString:@"*"]];
             [text addAttributes:@{NSFontAttributeName:wFontSize(self.fontS)} range:[text.string rangeOfString:text.string]];
         }else{
             [text addAttributes:@{NSFontAttributeName:wFontSize(self.fontS)} range:[text.string rangeOfString:text.string]];
         }
         self.attributedPlaceholder = text;
    }
}

- (UIView *)line{
    if (!_line) {
        _line = UIView.new;
        _line.backgroundColor = wColor_line;
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(PostK1px);
            make.bottom.left.right.mas_equalTo(0);
        }];
    }
    return _line;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    if (newSuperview) {
        self.delegate = self;
    }
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds{
    CGRect rect =  [super leftViewRectForBounds:bounds];
    if (!had) {
        rect.origin.x+=PostNum(20);
        rect.size.width+=PostNum(0);
        rect.size.height+=PostNum(0);
        rect.origin.y-=PostNum(5);
        if ([[UIDevice currentDevice].systemVersion doubleValue]<11.0) {
             had = YES;
        }
    }
    return rect;
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds{
    CGRect rect =  [super rightViewRectForBounds:bounds];
    if (!had) {
       rect.origin.x-=PostNum(50);
       rect.size.width+=PostNum(0);
       rect.size.height+=PostNum(10);
       rect.origin.y-=PostNum(5);
       if ([[UIDevice currentDevice].systemVersion doubleValue]<11.0) {
           had = YES;
       }
    }
    return rect;
}

- (CGRect)textRectForBounds:(CGRect)bounds{
    if (!_leftOffset) {
        return bounds;
    }
    return CGRectInset(bounds, PostNum(80), 0);
    
}

- (CGRect)editingRectForBounds:(CGRect)bounds{
    if (!_leftOffset) {
        return bounds;
    }
    return CGRectInset(bounds, PostNum(80), 0);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *mainText = @"";
    if ([string isEqual:@""]) {
        if (![textField.text isEqualToString:@""]) {
            mainText = [textField.text substringToIndex:[textField.text length] - 1];
        }else{
            mainText = textField.text;
        }
    }else{
        mainText = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }
    
    if ([string isEqualToString:@"\n"]&&
        (textField.returnKeyType == UIReturnKeySearch)){
        if (self.inputBlock) {
            self.inputBlock(YES, textField.text);
        }
        return NO;
    }
    if (self.maxNum&&mainText.length>self.maxNum) {
        return NO;
    }
    if (self.inputBlock) {
        self.inputBlock(NO, mainText);
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (self.notEdit) {
        if (self.clickBlock) {
            self.clickBlock(YES);
        }
    }
    return !self.notEdit;
}

@end
